﻿using System;
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using LuckyFish.ToDo.AddViews;
using LuckyFish.ToDo.Data.Model;
using LuckyFish.ToDo.ToDoViews;
using LuckyFish.ToDo.ViewModels;

namespace LuckyFish.ToDo;

public class ViewTemplate : IDataTemplate
{
    public Control Build(object? param)
    {
        
        if (param is ViewModelBase)
        {
            var name = param.GetType().FullName!.Replace("ViewModel", "View");
            var type = Type.GetType(name);

            if (type != null)
                return (Control)Activator.CreateInstance(type)!;
            return new TextBlock { Text = "Not Found: " + name };
        }
        
        if (param is string s)
        {
            if (s == "AddGroup")
                return new AddGroupView();

            return new TextBlock();
        }

        if (param is ToDoGroupModel)
            return new GroupView { DataContext = param };

        if (param is ToDoModel)
            return new ToDoView { DataContext = param };

        return new TextBlock();
    }

    public bool Match(object? data) => true;
}